package com.legleg.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by IntelliJ IDEA.
 * User: Sufeng
 * Date: 12-4-21
 * Time: 上午10:24
 * To change this template use File | Settings | File Templates.
 */
public class RequestUtil {
    public static int getInt(HttpServletRequest request,String param, int defaultValue){
        return NumberUtils.toInt(request.getParameter(param), defaultValue);
    }
    public static long getLong(HttpServletRequest request,String param, long defaultValue){
        return NumberUtils.toLong(request.getParameter(param), defaultValue);
    }

    public static double getDouble(HttpServletRequest request,String param, double defaultValue){
        return NumberUtils.toDouble(request.getParameter(param), defaultValue);
    }

    public static float getFloat(HttpServletRequest request,String param, float defaultValue){
        return NumberUtils.toFloat(request.getParameter(param), defaultValue);
    }
    public static String getStr(HttpServletRequest request, String param, String defaultValue){
        String s = request.getParameter(param);
        return s==null?defaultValue:s;
    }

    public static String getCookie(String cookieName, HttpServletRequest request) {
   		Cookie[] cookies = request.getCookies();
   		if (null != cookies)
   			for (Cookie cookie : cookies) {
   				if (cookieName.equals(cookie.getName()))
   					return cookie.getValue();
   			}
   		return null;
   	}

   	public static void deleteCookie(String cookiename, HttpServletResponse response) {
   		Cookie newCookie = new Cookie(cookiename, null);
   		newCookie.setMaxAge(0);
   		newCookie.setPath("/");
   		response.addCookie(newCookie);
   	}

    public static void setCookie(String cookieName, String cookieValue,
   			int cookieAge, String cookiePath, HttpServletResponse response) {
   		Cookie c = new Cookie(cookieName, cookieValue);
   		c.setMaxAge(cookieAge);
   		c.setPath(cookiePath);
   		response.addCookie(c);
   	}
    
    public static String toMD5(String plaintext) {
        if(StringUtils.trimToNull(plaintext)==null) return null;
        StringBuffer sb=new StringBuffer();
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(plaintext.getBytes());
            byte digested[] = algorithm.digest();
            
            for (int i = 0; i < digested.length; i++) {
                String hex=Integer.toHexString(0xff & digested[i]);
                if(hex.length()==1) 
                    sb.append("0");
                sb.append(hex);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
